package com.sxjh.controller.ny;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxjh.common.BaseController;
import com.sxjh.common.Constant;
import com.sxjh.common.Result;
import com.sxjh.common.ZPage;
import com.sxjh.entity.Unlock;
import com.sxjh.entity.param.IntruduListParam;
import com.sxjh.entity.param.UnlockListParam;
import com.sxjh.entity.param.UnlockParam;
import com.sxjh.entity.result.UnlockListResult;
import com.sxjh.service.PdfService;
import com.sxjh.service.UnlockService;
import com.sxjh.uitl.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.LinkedHashSet;
import java.util.List;

/**
 * <p>
 * 开锁
 * </p>
 *
 * @author guanxy
 * @since 2023-06-03
 */
@Slf4j
@Api(tags = "开锁接口")
@RestController
@RequestMapping("/unlock")
public class UnlockController extends BaseController {

    @Autowired
    private ExcelUtils excelUtils;
    @Autowired
    private PdfService pdfService;

    @Autowired
    private UnlockService unlockService;

    @Autowired
    private HttpServletResponse response;

    @ApiOperation("开锁记录保存")
    @PostMapping(value = "/save")
    public Result<Object> save(@RequestBody UnlockParam params) {
        log.error("========开锁记录保存参数:{}", JSON.toJSONString(params));

        Unlock unlock = new Unlock();
        unlock.setAddr(params.getAddr());
        unlock.setName(params.getName());
        unlock.setType(params.getType());
        unlock.setTypeName(params.getTypeName());
        unlock.setCreatedAt(params.getCreatedAt());
        unlock.setIsPush(2);
        unlockService.save(unlock);
        unlockService.pushNotice(unlock.getId());

        // 设置未读开锁列表查询参数
        UnlockListParam param = new UnlockListParam();
        param.setReaded(2);
        param.setIsPush(2);
        // 查询未读开锁列表
        Result<Object> result = unlockList(param);
        ZPage<UnlockListResult> data = (ZPage<UnlockListResult>) result.getData();
        if (data != null) {
            List<UnlockListResult> records = data.getRecords();
            records.forEach(t -> {
                // 更新开锁推送状态
                Unlock unlock1 = new Unlock();
                unlock1.setIsPush(1);
                unlock1.setId(t.getId());
                unlockService.updateById(unlock1);
            });

        }
        return Result.ok(HttpStatus.OK);
    }

    @ApiOperation("开锁列表")
    @PostMapping(value = "/unlockList")
    public Result<Object> unlockList(@RequestBody UnlockListParam param) {
        IPage iPage = new Page(param.getCurrent(), param.getSize());
        if (param.getSize() == -1) {
            iPage.setSize(Constant.maxSize);
        }
        iPage = unlockService.list(iPage, param);
        ZPage<UnlockListResult> zPage = copyPage(iPage, UnlockListResult::new);
        return Result.ok(zPage);
    }

    @PostMapping(value = "/getExport")
    @ApiOperation("开锁记录导出excel/pdf")
    public void getExport(@RequestBody UnlockListParam param) {
        param.setSize(-1);
        Result<Object> result = unlockList(param);
        ZPage<UnlockListResult> zPage = (ZPage<UnlockListResult>) result.getData();

        LinkedHashSet<String> headerFields = param.getHeaderFields();
        if (CollUtil.isEmpty(headerFields)) err("导出表头不能为空");
        List<UnlockListResult> records = zPage.getRecords();
        if (CollUtil.isEmpty(records)) err("导出数据不能为空");

        // 把导出结果为null的值替换成""空字符串
        for (int i = 0; i < records.size(); i++) {
            UnlockListResult unlockListResult = records.get(i);
            unlockListResult.setIndex(i + 1);
            if (StringUtils.isBlank(unlockListResult.getName())) {
                unlockListResult.setName("");
            }
            if (StringUtils.isBlank(unlockListResult.getAddr())) {
                unlockListResult.setAddr("");
            }
        }

        Integer flag = param.getFlag();
        if (flag == 1) {
            excelUtils.downLoadFromWeb(response, "开锁记录表", headerFields, records, "模板", UnlockListResult.class);
        } else if (flag == 2){
            pdfService.getPdf(response, "开锁记录表", headerFields,  records,"开锁记录表");
        }

    }

    @ApiOperation("开锁记录已读修改")
    @PostMapping(value = "/receipt")
    public Result<Void> receipt(@RequestBody IntruduListParam param) {
        if (CollUtil.isEmpty(param.getIds())) err("已读id列表不能为空");
        unlockService.receipt(param.getIds());
        unlockService.pushNotice(null);
        return Result.ok();
    }
}
